package com.echo.code.thread.mode.eda;

import java.util.concurrent.ExecutorService;

/**
 * AsyncEventHandler Created by baiyap on 2018/9/24.
 */
public abstract class EventHandler implements Channel<Event> {

  private final ExecutorService executorService;

  public EventHandler(ExecutorService executorService) {
    this.executorService = executorService;
  }

  // 重写dispatch方法, 并且用final修饰, 避免子类重写
  @Override
  public final void dispatch(Event event) {
    executorService.submit(() -> handle(event));
  }

  // 提供抽象方法, 供子类实现具体的Event处理
  protected abstract void handle(Event event);

  // 提供关闭ExecutorService的方法
  public void stop() {
    executorService.shutdown();
  }
}